package com.donfu.web.controller.house;

import java.util.List;

import com.donfu.common.utils.StringUtils;
import com.donfu.system.domain.dto.HouseGoodsDTO;
import io.swagger.annotations.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.donfu.common.annotation.Log;
import com.donfu.common.core.controller.BaseController;
import com.donfu.common.core.domain.AjaxResult;
import com.donfu.common.enums.BusinessType;
import com.donfu.system.domain.house.HouseGoods;
import com.donfu.system.service.house.IHouseGoodsService;
import com.donfu.common.utils.poi.ExcelUtil;
import com.donfu.common.core.page.TableDataInfo;

/**
 * 商品信息Controller
 *
 * @author donfu
 * @date 2021-12-01
 */
@Api(tags = "后台--商品信息(spu商品集)")
@RestController
@RequestMapping("/house/goods")
public class HouseGoodsController extends BaseController {
    @Autowired
    private IHouseGoodsService houseGoodsService;

    /**
     * 查询商品信息列表
     */
    @ApiOperation("查询商品信息列表")
    @PreAuthorize("@ss.hasPermi('house:goods:list')")
    @GetMapping("/list")
    public TableDataInfo list(HouseGoods houseGoods) {
        startPage();
        List<HouseGoods> list = houseGoodsService.selectHouseGoodsList(houseGoods);
        return getDataTable(list);
    }

    /**
     * 导出商品信息列表
     */
    @ApiOperation("导出商品信息列表")
    @PreAuthorize("@ss.hasPermi('house:goods:export')")
    @Log(title = "商品信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HouseGoods houseGoods) {
        List<HouseGoods> list = houseGoodsService.selectHouseGoodsList(houseGoods);
        ExcelUtil<HouseGoods> util = new ExcelUtil<HouseGoods>(HouseGoods.class);
        return util.exportExcel(list, "商品信息数据");
    }

    /**
     * 获取商品信息详细信息
     */
    @ApiOperation("获取商品信息详细信息")
    @ApiImplicitParam(name = "id", value = "商品信息ID", dataType = "Long", required = true)
    @PreAuthorize("@ss.hasPermi('house:goods:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(houseGoodsService.selectHouseGoodsById(id));
    }

    /**
     * 新增商品信息
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('house:goods:add')")
    @Log(title = "商品信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HouseGoods houseGoods) {
        AjaxResult ajax = null;
        // 查询首页置顶的商品
        List<HouseGoodsDTO> houseGoodsList = houseGoodsService.getTopGoods();
        // 上架的首页置顶的商品不能超过5个
        if (StringUtils.isNotEmpty(houseGoodsList)) {
            if (houseGoodsList.size() >= 5) {
                if (houseGoods.getSaleable() == 1 && houseGoods.getIsIntdex() == 1) {
                    ajax = AjaxResult.error();
                    ajax.put("msg", "上架的首页置顶的商品不能超过5个");
                    return ajax;
                }
            }
        }
        return toAjax(houseGoodsService.insertHouseGoods(houseGoods));
    }

    /**
     * 修改商品信息
     */
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('house:goods:edit')")
    @Log(title = "商品信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HouseGoods houseGoods) {
        AjaxResult ajax = null;
        // 是否上架， 0下架，1上架
        HouseGoods goods = houseGoodsService.selectHouseGoodsById(houseGoods.getId());
        if (goods.getSaleable() == 1) {
            ajax = AjaxResult.error();
            ajax.put("msg", "上架的商品不能进行修改操作,请先下架!");
            return ajax;
        }
        // 查询首页置顶的商品
        List<HouseGoodsDTO> houseGoodsList = houseGoodsService.getTopGoods();
        // 首页置顶商品不能多于五个
        if (StringUtils.isNotEmpty(houseGoodsList)) {
            if (houseGoodsList.size() >= 5) {
                for (HouseGoodsDTO houseGoodsDTO : houseGoodsList) {
                    // 更新的商品是置顶中的商品，则可以直接更新
                    if (houseGoods.getId().equals(houseGoodsDTO.getId())) {
                        return toAjax(houseGoodsService.updateHouseGoods(houseGoods));
                    }
                }
                // 更新的商品是不属于置顶中的商品，则需要商品更新后的置顶状态不为1才能做更
                if (houseGoods.getIsIntdex() == 1) {
                    ajax = AjaxResult.error();
                    ajax.put("msg", "上架的首页置顶的商品不能超过5个");
                    return ajax;
                }
            }
        }
        return toAjax(houseGoodsService.updateHouseGoods(houseGoods));
    }

    /**
     * 删除商品信息
     */
    @ApiOperation("删除")
    @ApiImplicitParam(name = "ids", value = "商品信息ID集合", dataType = "Long", required = true)
    @PreAuthorize("@ss.hasPermi('house:goods:remove')")
    @Log(title = "商品信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        AjaxResult ajax = null;
        for (Long id : ids) {
            HouseGoods houseGoods = houseGoodsService.selectHouseGoodsById(id);
            // 是否上架，0下架，1上架
            if (houseGoods.getSaleable() == 1) {
                ajax = AjaxResult.error();
                ajax.put("msg", "上架的商品不能进行删除操作,请先下架!");
                return ajax;
            }
        }

        return toAjax(houseGoodsService.deleteHouseGoodsByIds(ids));
    }

    /**
     * 商品上下架和是否置顶首页
     *
     * @param goodsId  商品信息ID
     * @param saleable 商品是否上下架，0下架，1上架
     * @return
     */
    @ApiOperation("商品上下架和是否置顶首页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId", value = "商品信息ID", dataType = "Long", required = true),
            @ApiImplicitParam(name = "saleable", value = "商品是否上下架，0下架，1上架", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "isIndex", value = "商品是否要首页置顶，0不置顶，1置顶", dataType = "Integer", required = true)
    })
    @PreAuthorize("@ss.hasPermi('house:goods:saleable')")
    @Log(title = "商品信息", businessType = BusinessType.UPDATE)
    @PostMapping("/saleable/{goodsId}/{saleable}/{isIndex}")
    public AjaxResult saleable(@PathVariable("goodsId") Long goodsId,
                               @PathVariable("saleable") Integer saleable,
                               @PathVariable("isIndex") Integer isIndex) {
        AjaxResult ajax = null;
        // 查询置顶的商品列表
        List<HouseGoodsDTO> topGoods = houseGoodsService.getTopGoods();
        // 商品要上架
        if (saleable == 1) {
            // 首页置顶商品不能多于五个
            if (StringUtils.isNotEmpty(topGoods) && topGoods.size() >= 5) {
                // 更新的商品是不属于置顶中的商品，则需要商品更新后的置顶状态不为1才能做更
                if (isIndex == 1) {
                    ajax = AjaxResult.error();
                    ajax.put("msg", "上架的首页置顶的商品不能超过5个");
                    return ajax;
                }
            }
        }
        HouseGoods houseGoods = new HouseGoods();
        houseGoods.setId(goodsId);
        houseGoods.setSaleable(saleable);
        houseGoods.setIsIntdex(isIndex);
        return toAjax(houseGoodsService.updateHouseGoods(houseGoods));
    }



}
